<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends Admin_Controller {
	protected $list_view_order_name = 'created';
	protected $list_view_order_direction = 'DESC';
	protected $list_view_pagination = array(20,50,100);
	protected $list_view_searched_fields = array(
			'title'			=>	'Title',
		);
	protected $fields =  array(
				'status'			=>	array(
											'text'	=>	'Status',
											'type'	=>	'checkbox',
											'form_view'=>array()
										),
				'title'				=>	array(
											'text'	=>	'Title',
											'type'	=>	'text',
											'list_view'=>array(
												'is_show_link'	=>	true
											),
											'form_view'=>array(
												'validate_type'	=>	'required : true'
											)
										),
				'price_vn'			=>	array(
										'text'	=>	'Price (VND)',
										'type'	=>	'text',
										'list_view'=>array(
											'is_show_link'	=>	true
										),
										'form_view'=>array(
											'validate_type'	=>	'required : true'
										)
									),
				'description'		=>	array(
										'text'	=>	'Description',
										'type'	=>	'textarea',
										'form_view'=>array(
											'editor'	=>	true
										)
									),						
				'image_small'		=>	array(
										'text'	=>	'Image (160x146)',
										'type'	=>	'file',
										'file_config' => array(
											'file_type' => 'image',
											'file_height' => 146,
											'file_dir_path' => 'uploads/product/image/160x146/'
										),
										'list_view'=>array(
											'width' => 100,
											'file_height' => 73,
											'is_show_link'	=>	true
										),
										'form_view' => array(
										)
									),
				'image_large'		=>	array(
										'text'	=>	'Image (323x213)',
										'type'	=>	'file',
										'file_config' => array(
											'file_type' => 'image',
											'file_height' => 213,
											'file_dir_path' => 'uploads/product/image/323x213/'
										),
										'form_view' => array(
										)
									)
		);

	/** End: Config **/

	/*------------------------------------ Admin Control Panel ------------------------------------*/
	
	protected function after_save($obj_id){
	}
	/*----------------------------- End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	function index(){
		$data = array();
		$auction_list = $this->model->fetch('*','tb_product','status = 1','created','desc',-1,0,true);
		$data['auction_list'] = $auction_list;
		if(!empty($_SESSION['order_success_message'])){
			$data['order_success_message'] = $_SESSION['order_success_message'];
			unset($_SESSION['order_success_message']);
		}
		$this->template->write_view('content','FRONTEND/index',$data);
		$this->template->render();
	}
	
	function detail($product_id=0){
		$product_id = (int)$product_id;
		if(!empty($product_id)){
			$product = $this->model->get('*','tb_product',"id = {$product_id}",true);
			if(!empty($product)){
				$data['product'] = $product;
				$this->template->write_view('content','FRONTEND/detail',$data);
				$this->template->render();
			}
		}
	}
	function buy_submit(){
		$redirect_url = PATH_URL;
		if(!empty($_SESSION['user'])){
			$user = $_SESSION['user'];
			$user_id = $user['id'];
			$product_id = (int)$this->input->post('product_id');
			if(!empty($product_id)){
				$product = $this->model->get('*','tb_product',"id = {$product_id}",true);
				if(!empty($product)){
					$order = !empty($_SESSION['order']) ? $_SESSION['order'] : array();
					$order_detail_list = !empty($order['order_detail_list']) ? $order['order_detail_list'] : array();
					$check_duplicate = false;
					foreach($order_detail_list as $order_detail){
						if(!empty($order_detail['order_detail_product'])){
							$order_detail_product = $order_detail['order_detail_product'];
							if($order_detail_product['id'] == $product_id){
								$check_duplicate = true;
								break;
							}
						}
					}
					if(!$check_duplicate){
						$order_detail['order_detail_product'] = $product;
						$order_detail['product_id'] = $product_id;
						$order_detail['quantity'] = 1;
						$order_detail_list[] = $order_detail;
						$order['order_detail_list'] = $order_detail_list;
						$_SESSION['order'] = $order;
					}
					$redirect_url = PATH_URL.'mua-hang/gio-hang';
				}
			}
		}
		redirect($redirect_url);
	}
	
	function order(){
		if(!empty($_SESSION['user']) && !empty($_SESSION['order'])){
			$user = $_SESSION['user'];
			// $user_id = $user['id'];
			$order = $_SESSION['order'];
			$data['user'] = $user;
			$data['order'] = $order;
			if(!empty($_SESSION['order_error_message'])){
				$data['order_error_message'] = $_SESSION['order_error_message'];
				unset($_SESSION['order_error_message']);
			}
			$this->template->write_view('content','FRONTEND/order',$data);
			$this->template->render();
		} else {
			redirect(PATH_URL);
		}
	}
	function order_submit(){
		$redirect_url = PATH_URL;
		if(!empty($_SESSION['user'])){
			$user = $_SESSION['user'];
			$user_id = $user['id'];
			$user_data = $this->model->get('id,coin_number',ADMIN_USER_TABLE,"id = {$user_id}",true);
			
			$order = !empty($_SESSION['order']) ? $_SESSION['order'] : array();
			if(!empty($order) && !empty($user_data)){
				$order_detail_list = !empty($order['order_detail_list']) ? $order['order_detail_list'] : array();
				
				// Check amout of coins
				$coin_count = 0;
				foreach($order_detail_list as &$order_detail){
					if(!empty($order_detail['order_detail_product'])){
						$order_detail_product = $order_detail['order_detail_product'];
						$product_id = $order_detail_product['id'];
						$quantity = (int)$this->input->post('quantity_'.$product_id);
						$order_detail_coin = get_coin_by_price_for_product($order_detail_product,false)*$quantity;
						$coin_count += $order_detail_coin;
					}
				}
				if($user_data['coin_number'] >= $coin_count){
					$order_data['user_id'] = $user_id;
					$order_data['created'] = getNow();
					if($this->model->insert('tb_order',$order_data)){
						$order_id = $this->db->insert_id();
						$order_result = true;
						foreach($order_detail_list as $order_detail){
							if(!empty($order_detail['order_detail_product'])){
								$order_detail_product = $order_detail['order_detail_product'];
								$product_id = $order_detail_product['id'];
								$quantity = (int)$this->input->post('quantity_'.$product_id);
								$order_detail_data['order_id'] = $order_id;
								$order_detail_data['product_id'] = $product_id;
								$order_detail_data['quantity'] = $quantity;
								$order_detail_data['status'] = 1;
								$order_detail_data['created'] = getNow();
								if(!$this->model->insert('tb_order_detail',$order_detail_data)){
									$order_result = false;
									break;
								}
							}
						}
						if($order_result){
							// Update user's coin
							$user_data['coin_number'] = $user_data['coin_number'] - $coin_count;
							$this->model->update('tb_user',$user_data,"id = {$user_id}");
							
							unset($_SESSION['order']); // Clear session
							$_SESSION['order_success_message'] = 'Xin chúc mừng bạn đã thanh toán thành công!';
							$redirect_url = PATH_URL.'mua-hang';
						}
					}
				} else {
					$redirect_url = PATH_URL.'mua-hang/gio-hang';
					$_SESSION['order_error_message'] = 'Không đủ xu để thanh toán!';
				}
			}
		}
		redirect($redirect_url);
	}
	/*------------------------------------ End FRONTEND --------------------------------*/
}